package com.chinatgl.latte.ec.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chinatgl.latte.app.Latte;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.mode.Product;

import java.util.ArrayList;
@SuppressWarnings("all")
public class ShopGoodsRecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    ArrayList<Product.ProductBean> list;
    private int type = 0;//0:LinearViewHolder  1:GridViewHolder


    public ShopGoodsRecycleViewAdapter(ArrayList<Product.ProductBean> objects) {

        this.list = objects;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View baseView;

        baseView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_list, parent, false);
        GridViewHolder gridViewHolder = new GridViewHolder(baseView);
        return gridViewHolder;

    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        Product.ProductBean product = null;
        if (list == null) {
            return;
        }
        product = list.get(position);
        final String id = product.getId();
        final String shopId = product.getShopId();
        final String brandId = product.getBrandId();

        final GridViewHolder gridViewHolder = (GridViewHolder) holder;


        gridViewHolder.goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.click(id, shopId, brandId);
                }
            }
        });
        final Product.ProductBean finalProduct = product;
        gridViewHolder.ivIove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (listener != null) {

                    listener.loveClick(id, gridViewHolder.ivIove, finalProduct.getIsCollect(), position);
                }
            }
        });


        gridViewHolder.tvName.setText(product != null ? product.getName() : "测试商品,请勿购买");
        gridViewHolder.tv_fanli.setText(product.getRebate() != null ? product.getRebate() : "0");
        gridViewHolder.tvPrice.setText((product != null ? product.getPrice() : "0") + "");

        Glide.with(Latte.getApplication())
                .load(product != null ? product.getImage() + "?imageView2/q/50" : "0")
              //  .placeholder(R.mipmap.img_zhanwei_pinpai)
                .error(R.mipmap.img_zhanwei_pinpai)
                .into(gridViewHolder.iv);

        //  gridViewHolder.ivIove
        String isCollect = product.getIsCollect();
        if (isCollect != null) {
            Glide.with(Latte.getApplication())
                    .load(isCollect.equals("0") ? R.mipmap.icon_love_nor : R.mipmap.icon_love_pre)
                    .into(gridViewHolder.ivIove);
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public static class GridViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout goods;
        private ImageView iv, ivIove;
        private TextView tvName, tv_fanli, tvPrice;


        public GridViewHolder(View itemView) {
            super(itemView);
            goods = (LinearLayout) itemView.findViewById(R.id.goods);
            iv = (ImageView) itemView.findViewById(R.id.imageview);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tv_fanli = (TextView) itemView.findViewById(R.id.tv_fanli);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            ivIove = (ImageView) itemView.findViewById(R.id.iv_love);


        }
    }

    public interface onItemClick {
        void click(String id, String shopId, String brandId);

        void loveClick(String id, ImageView view, String isCollect, int position);
    }

    private onItemClick listener;

    public void setOnitemClick(onItemClick listener) {

        this.listener = listener;
    }
}
